package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import java.util.Collection;

import org.apache.log4j.Category;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.user.Person;

public class RecruiterComboTag extends PeopleComboTag {

	private static final long serialVersionUID = -3328559532789821818L;
	
	static private Category log = Category.getInstance(RecruiterComboTag.class);
    private String addNoPersonOption = null;
    private String jobId = null;
    private String companyId = null;

    
	/**
	 * @return Returns the companyId.
	 */
	public String getCompanyId() {
		return companyId;
	}

	/**
	 * @param companyId The companyId to set.
	 */
	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	protected boolean getAddNoPersonOption() {
		boolean result = false;
		if (addNoPersonOption.equals("true")) {
			result = true;
		} else if (addNoPersonOption.equals("false")) {
			result = false;
		} else {
			throw new SystemException("Illegal value for 'addNotPersonOption' needs to be 'true' or 'fale' - value is " + addNoPersonOption);
		}
		return(result);
	}

	public void setAddNoPersonOption(String value) {
		this.addNoPersonOption = value;
	}
	
	protected Collection findPersons() {
		log.debug("findPersons() - called");
		Company company = CoreServiceLocator.getCompanyService().findCompany(new Long(this.getCompanyId()));
		Collection persons = RecruitServiceLocator.getRecruitCompanyService().findRecruiters(company);
		return(persons);
	}
	
	protected Long getSelectedPersonId() {
		if (getProperty() == null) {
			log.error("getSelectedUserId() - param called 'property' is NULL and should have a value");
			throw new DataException("getSelectedUserId() - param called 'property' is NULL and should have a value");
		}
			
		log.debug("getSelectedUserId()");
		Job job = null;
		if (jobId != null) {
			job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
		}

		if (job == null) {
			log.debug("getSelectedUserId() - Could not find job with id");
		}
		else {
			log.debug("getSelectedUserId() - Found job with id");
		}

		log.debug("getSelectedUserId() - finding employee");
		Person employee = null;
		if (job != null) {
			if (getProperty().toLowerCase().indexOf("prim") >= 0) {
				log.info("getSelectedUserId() - finding primary recruiter");
				employee = job.getPrimaryRecruiter();
			}
			else {
				log.info("getSelectedUserId() - finding secondary recruiter");
				employee = job.getSecondaryRecruiter();
			}
		}
		
		log.debug("getSelectedUserId() - finding employee id");
		Long employeeUserId = null;
		if (employee != null) {
			log.debug("employee found, recording id of employee");
			employeeUserId = employee.getId();
		}
		else {
			log.debug("employee not found");
		}
		return(employeeUserId);		
	}

	/**
	 * @return
	 */
	public String getJobId() {
		return jobId;
	}

	/**
	 * @param jobId
	 */
	public void setJobId(String jobId) {
		log.debug("setJobId() - jobId = " + jobId);
		this.jobId = jobId;
	}

}
